import * as userApi from '@/api/user'
import dayjs from 'dayjs'
import { getStorage } from '@/utils/storage'
Page({
  data: {
    begin: '2024-10-25',
    end: '2024-10-30',
    content: '1.css2.html3.jshhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh',
    fileList: [],
    disabled: false,
    userInfo: getStorage('userInfo')
  },
  onLoad() {
    this.getInformation()
  },
  getInformation() {
    userApi.reqCurTest(this.data.userInfo.testId).then(res => {
      this.setData({
        begin: dayjs(res.data.begin).format('YYYY-MM-DD'),
        end: dayjs(res.data.end).format('YYYY-MM-DD'),
        content: res.data.text,
        fileList: res.data.fileList,
        disabled: !(Date.now() > dayjs(res.data.begin).valueOf() && Date.now() < dayjs(res.data.end).valueOf())
      })
    })
  },
  // 点击下载文件
  downloadFile(e) {
    const url = e.currentTarget.dataset.url
    wx.downloadFile({
      url: url,
      success: (res) => {
        const filePath = res.tempFilePath
        wx.openDocument({
          filePath: filePath,
          showMenu:true,
          success: () => {
            console.log('打开成功')
          },
          fail: () => {
            console.log('打开失败')
          }
        })
      },
      fail: () => {
        console.log('下载失败')
      }
    })
  },
  // 预览图片
  previewImg(e) {
    const url = e.currentTarget.dataset.url
    wx.previewImage({
      current: url,
      urls: [url],
    })
  },
  appoint() {
    wx.navigateTo({
      url: '/pages/appoint/appoint',
    })
  }
})